// pages/alarmprocessor/alarmprocessor.ts
import { processor } from '../../api/site-monitoring'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProcessRecords: '' as String,
    inputOption: {
      maxHeight: 500,
      minHeight: 200
    },
    fileList: [],
    isLoading: false as Boolean,
    type: -1 as Number | String,
    id: -1 as Number | String,
    userId: -1 as Number,
    imgUrl:'' as String
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    this.setData({
      type: option.type,
      id: option.id,
      
    })
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    })
  },
  //上传图片
  afterRead(event: any) {
    const { file } = event.detail;
    const _this = this
    const { fileList = [] } = _this.data;
    fileList.push({ ...file, url: file.url });
    _this.setData({ fileList });

  },
  deleteImg(e: any) {
    const index: number = e.detail.index
    this.data.fileList.splice([index], 1)
    this.setData({
      fileList: this.data.fileList
    });
    let img_list = [];
    this.data.fileList.forEach((e: any) => {
      img_list.push(e.url)
    });
  },
  async onSubmit() {
    const _this = this
    const type = this.data.type;
    const id = this.data.id;
    const userID = this.data.userId
    const url = this.data.fileList[0].url
    const imgUrl = this.data.imgUrl
    const ProcessRecords = this.data.ProcessRecords
    this.setData({
      isLoading: true
    })
    if (this.data.ProcessRecords && this.data.fileList.length != 0) {
      // const imgUrl = JSON.parse(this.data.fileList[0].url).msg
      await wx.uploadFile({
        // url: 'http://192.168.2.48:8080/MobileLinkCenter/picture/pictureUpload', // 仅为示例，非真实的接口地址
        url: 'https://www.hbdinkon.cn:10020/MobileLinkCenter/picture/pictureUpload',
        filePath: url,
        name: 'file',
        success(res) {
          const imgUrl = JSON.parse(res.data).msg
         processor(type, id, userID, ProcessRecords, imgUrl).then((res: any) => {
            if (res.data.code == 200) {
              wx.showToast({
                title: res.data.message,
                icon: 'success'
              })
              _this.setData({
                isLoading: false
              })
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }else{
              _this.setData({
                isLoading: false
              })
            }
          })
        },
      });
    } else {
      wx.showToast({
        title: '缺少必填项',
        icon: 'error'
      })
      this.setData({
        isLoading: false
      })

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userId: wx.getStorageSync("userInfo").data.userID
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})